﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OpenDoor : MonoBehaviour
{
    private bool isInRange = false;
    private bool pressKey = false;
    private Animator animator;      //动画器引用
    // Start is called before the first frame update
    void Start()
    {
        animator = transform.parent.GetComponent<Animator>();    //获取动画器
    }

    // Update is called once per frame
    void Update()
    {
        if (isInRange)
        {
            if(Input.GetKey(KeyCode.E))
            {
                if (pressKey == false)
                {
                    animator.SetBool("Open", true);
                    pressKey = true;
                }
                else
                {
                    animator.SetBool("Open", false);
                    pressKey = false;
                }
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.tag.Equals("Player"))
        {
            //print("开门");
            //animator.SetBool("Open", true);
            isInRange = true;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if(other.tag.Equals("Player"))
        {
            //animator.SetBool("Open", false);
            isInRange = false;
        }
    }
}
